/**
 * Created by lwy on 2017/6/13.
 */
var page=0;
var num=0;

$(function(){
    doAjax();
})

function doAjax(){
    page++;
    $.ajax({
        url:'/flowC/getDingDangFlow.json',
        dataType:'json',
        type:'post',
        data:{
            page:page,
            token:getItem('token'),
        },
        beforeSend:function(){
            if(page==1){
                $('<div class="loading"><img src="../phoneImg/jiazaizhong.gif"/></div>').appendTo($('#basePoint'));
            }
        },
        success:function(data){
            if(data.code==-1){
                window.location.href="login.html";
            }
            if(data.code==0){
                mui.toast(data.msg);
            }
            if(data.code==1){
                var data = data.datas;
                if(page==1){
                    $('#basePoint').empty();
                    if(data.length==0){
                        mui('#refreshContainer').pullRefresh().disablePullupToRefresh();
                        $('<div class="kong"><img src="../img/kong.png" alt="暂无数据"></div>').appendTo($('#basePoint'));
                    }
                }
                var capitalHtml = '';
                for(var i=0;i<data.length;i++){
                    capitalHtml+='<div class="capital-item1 mui-clearfix"><h3 class="capital-item1-time">'+data[i].createDate+'</h3>';
                    for(var a=0;a<data[i].details.length;a++){
                        capitalHtml+='<div class="mui-clearfix">'+
                            '<div class="capital-item1-left" id="1"><span class="line"></span><b></b><span class="line line2"></span></div>'+
                            '<div class="capital-item1-sanjiao"></div>'+
                            '<div class="capital-item1-right">'+
                            '<span class="time">'+data[i].details[a].CreateTime+'</span><span class="code">'+data[i].details[a].DingDangCode+'</span>'+
                            '<p class="line3"></p><table>'+
                            '<tr class="one-tr"><td>交易额</td><td>交易类型</td></tr>'+
                            '<tr><td class="money">'+data[i].details[a].Change_num+'</td><td class="explain">'+data[i].details[a].Source+'</td></tr>'+
                            '</table></div></div>'
                    }
                    capitalHtml+='</div>';
                }
                $('#basePoint').append(capitalHtml)
                num=data.length;
            }
        }
    })
    return num;
}

//上拉加载
mui.init({
    pullRefresh : {
        container:"#refreshContainer",//待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
        up : {
            contentrefresh : "正在加载...",//可选，正在加载状态时，上拉加载控件上显示的标题内容
            contentnomore:'我是有底线的',//可选，请求完毕若没有更多数据时显示的提醒内容；
            callback :function(){
                setTimeout(function () {
                    var numResult = doAjax();;
                    if (numResult < 20) {
                        mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
                    } else {
                        mui('#refreshContainer').pullRefresh().endPullupToRefresh(false);
                    }
                }, 1000);
            } //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
        }
    }
});
